/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.business.bean.CarBlacklistBean;
import com.rf.business.dao.CarBlacklistMapper;
import com.rf.business.model.CarBlacklist;
import com.rf.business.model.CarBlacklistExample;
import com.rf.business.service.CarBlacklistService;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.DepUser;
import com.rf.sys.model.Department;
import com.rf.sys.model.User;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.service.UserService;
import com.rf.sys.utils.PageUtils;

@Service("carBlacklistService")
public class CarBlacklistServiceImpl implements CarBlacklistService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private CarBlacklistMapper carBlacklistMapper;//注入dao

	@Resource
	private DepartmentService departmentService;
	@Resource
	private UserService userService;
   	/**
	 * 根据id获取数据
	 * @param carBlacklistId
	 * @return CarBlacklist
	 */
   	@Override
	public CarBlacklist getCarBlacklistByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return carBlacklistMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据CarBlacklist获取数据
	 * @param CarBlacklist
	 * @return List<CarBlacklist>
	 */
	@Override
	public List<CarBlacklist> getCarBlacklist(CarBlacklist carBlacklist) throws Exception{
		// TODO Auto-generated method stub
		CarBlacklistExample example = new CarBlacklistExample();
		com.rf.business.model.CarBlacklistExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<CarBlacklist> list = carBlacklistMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<CarBlacklist>
	 */
	@Override
	public List<CarBlacklist> getCarBlacklist() throws Exception{
		// TODO Auto-generated method stub
		CarBlacklistExample example = new CarBlacklistExample();
		com.rf.business.model.CarBlacklistExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return carBlacklistMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param CarBlacklist
	 * @return
	 */
	@Override
	public void addCarBlacklist(CarBlacklist carBlacklist) throws Exception{
		// TODO Auto-generated method stub
		carBlacklistMapper.insert(carBlacklist);
	}

	/**
	 * 修改数据
	 * @param CarBlacklist
	 * @param
	 */
	@Override
	public void updateCarBlacklist(CarBlacklist carBlacklist) throws Exception{
		// TODO Auto-generated method stub
		carBlacklistMapper.updateByPrimaryKeySelective(carBlacklist);
	}
	
	/**
	 * 删除数据
	 * @param carBlacklistId
	 * @param
	 */
	@Override
	public void deleteCarBlacklist(String id
) throws Exception{
		// TODO Auto-generated method stub
		carBlacklistMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param CarBlacklist
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getCarBlacklistPage(CarBlacklist carBlacklist,PageUtils pageUtils) throws Exception{
		CarBlacklistExample example = new CarBlacklistExample();
		com.rf.business.model.CarBlacklistExample.Criteria criteria= example.createCriteria();
		Session session = SecurityUtils.getSubject().getSession();
		String depId = (String)session.getAttribute("depSessionId");
		if(depId !=null  && !depId.equals("")){
				criteria.andDepIdEqualTo(depId);
		}
		
		if(carBlacklist.getCarNo()!=null && !carBlacklist.getCarNo().equals("")){
			criteria.andCarNoLike("%"+carBlacklist.getCarNo()+"%");
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<CarBlacklist> list = carBlacklistMapper.selectByExample(example);
		List<CarBlacklistBean> listBean =  new ArrayList<CarBlacklistBean>();
		if(list != null && list.size() > 0){
			for(CarBlacklist n:list){
				User cUser = userService.getUserByID(n.getCreateUser()) ;
				Department dep = departmentService.getDepartmentByID(n.getDepId());
				CarBlacklistBean cBLB = new CarBlacklistBean();
				cBLB.setId(n.getId());
				cBLB.setDepId(n.getDepId());
				cBLB.setCarNo(n.getCarNo());
				cBLB.setBeginDate(n.getBeginDate());
				cBLB.setEndDate(n.getEndDate());
				cBLB.setState(n.getState());
				cBLB.setRemark(n.getRemark());
				cBLB.setDepName(dep.getName());
				cBLB.setUserName(cUser.getUserName());
				cBLB.setCreateTime(n.getCreateTime());
				listBean.add(cBLB);
			}
		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", listBean);
		return dateMap;
	}
}

